package com.obas.access.business.service.impl;

import java.util.List;

import com.ease.common.vo.MapParameter;
import com.ease.platform.access.service.impl.AbstractNamespaceAccessServiceImpl;
import com.obas.access.business.service.SystemFunctionAccessService;
import com.obas.access.business.service.TestcaseScriptAccessService;
import com.obas.model.business.SystemFunction;
import com.obas.model.business.TestcaseScript;
import com.obas.model.business.criteria.SystemFunctionCriteria;

public class SystemFunctionAccessServiceImpl extends AbstractNamespaceAccessServiceImpl<SystemFunction> implements SystemFunctionAccessService {

	static final class Statement {

		public static final String associateSystemUsecase = "associateSystemUsecase";
		public static final String disassociateSystemUsecase = "disassociateSystemUsecase";
		public static final String disassociateAllSystemUsecase = "disassociateAllSystemUsecase";
	}

	private TestcaseScriptAccessService testcaseScriptAccessService;

	public List<SystemFunction> findBySystemUsecaseId(long systemUsecaseId) {
		SystemFunctionCriteria parameter = new SystemFunctionCriteria();
		parameter.setSystemUsecaseId(systemUsecaseId);
		return this.find(parameter);
	}

	public SystemFunction createEntity(SystemFunction systemFunction, long systemUsecaseId) {
		systemFunction = this.create(systemFunction);
		if (systemUsecaseId > 0) {
			associateSystemUsecase(systemFunction.getId(), systemUsecaseId);
		}
		return systemFunction;
	}

	public SystemFunction remove(SystemFunction systemFunction) {
		if (null != systemFunction) {

			long systemFunctionId = systemFunction.getId();
			//
			disassociateAll(systemFunctionId, systemFunction);
			//
			systemFunction = super.remove(systemFunction);
			//
			removeCascade(systemFunctionId, systemFunction);
		}
		return systemFunction;
	}

	public void removeCascade(long systemFunctionId, SystemFunction systemFunction) {
		{/* TBD */
			TestcaseScript testcaseScript = testcaseScriptAccessService.findById(systemFunction.getTestcaseScriptId());
			testcaseScriptAccessService.remove(testcaseScript);
		}
	}

	public void disassociateAll(long systemFunctionId, SystemFunction systemFunction) {
		disassociateAllSystemUsecase(systemFunctionId);
	}

	public void associateSystemUsecase(long systemFunctionId, long systemUsecaseId) {
		MapParameter parameter = new MapParameter();
		parameter.put("systemFunctionId", systemFunctionId);
		parameter.put("systemUsecaseId", systemUsecaseId);
		this.update(Statement.associateSystemUsecase, parameter);
	}

	public void disassociateSystemUsecase(long systemFunctionId, long systemUsecaseId) {
		MapParameter parameter = new MapParameter();
		parameter.put("systemFunctionId", systemFunctionId);
		parameter.put("systemUsecaseId", systemUsecaseId);
		this.remove(Statement.disassociateSystemUsecase, parameter);
	}

	public void disassociateAllSystemUsecase(long systemFunctionId) {
		MapParameter parameter = new MapParameter();
		parameter.put("systemFunctionId", systemFunctionId);
		this.remove(Statement.disassociateAllSystemUsecase, parameter);
	}

	/**
	 * @return the testcaseScriptAccessService
	 */
	public TestcaseScriptAccessService getTestcaseScriptAccessService() {
		return testcaseScriptAccessService;
	}

	/**
	 * @param testcaseScriptAccessService
	 *            the testcaseScriptAccessService to set
	 */
	public void setTestcaseScriptAccessService(TestcaseScriptAccessService testcaseScriptAccessService) {
		this.testcaseScriptAccessService = testcaseScriptAccessService;
	}

}